﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Sinacor.UI.Bovespa.ManutencaoOrdens.Presenter;
using Sinacor.Infra.UI.Controls.SmartClient;
using Sinacor.Infra.UI.Common;

namespace Sinacor.UI.Bovespa.ManutencaoOrdens.View.SmartClient
{
    public partial class ErrosRecepcaoOfertasNegocios : SinacorWindow, IErroRecepcaoOfertaNegocio
    {
        #region Campos        
        //public Model.ErroRecepcaoNegocio CurrentModelNegocio
        //{
        //    get
        //    {         
        //       return (Model.ErroRecepcaoNegocio)this.dtgNegociosDisp_ApplyingStyle.SelectedItem;
        //    }
        //}
        //public Model.ErroRecepcaoOferta CurrentModelOferta
        //{
        //    get
        //    {         
        //       return (Model.ErroRecepcaoOferta)this.dtgOrdem.SelectedItem;
        //    }
        //}
        #endregion

        #region Presenter
        /// <summary>
        /// Casamento Manual
        /// </summary>
        public Presenter.ErroRecepcaoOfertaNegocio _presenter { get; set; }
        #endregion

        #region Construtor
        /// <summary>
        /// CadastroApelidoCliente
        /// </summary>
        public ErrosRecepcaoOfertasNegocios()
        {
            InitializeComponent();

            this._presenter = new Sinacor.UI.Bovespa.ManutencaoOrdens.Presenter.ErroRecepcaoOfertaNegocio(this);
            this.DataContext = this._presenter;

            this.dtgOrdem.NewItemType = typeof(Model.ErroRecepcaoOferta);
            this.dtgNegocio.NewItemType = typeof(Model.ErroRecepcaoNegocio);

            this.AfterInitialize += new RoutedEventHandler(CasamentoManual_AfterInitialize);
        }
                
        private void CasamentoManual_AfterInitialize(object sender, RoutedEventArgs e)
        {            
            base.ShowUserWaitingProcess();

            this._presenter.Inicializar();

            base.HideUserWaitingProcess();
        }
      
       #endregion

       

        #region ICasamentoManual Members
              
        #endregion



        #region IErroRecepcaoOfertaNegocio Members

        public void AtualizarListaErrosRecepcoesNegocios()
        {
            this.dtgNegocio.UpdateTarget(SinacorDataGrid.ItemsSourceProperty);
        }

        public void AtualizarListaErrosRecepcoesOfertas()
        {
            this.dtgOrdem.UpdateTarget(SinacorDataGrid.ItemsSourceProperty);
        }
        /// <summary>
        /// Atualiza combobox de calendário
        /// </summary>
        public void AtualizarCalendario()
        {
            this.cmbErrosRecepcaoOfertaNegocio.ItemsSource = _presenter.ListaCalendarios;
            this.cmbErrosRecepcaoOfertaNegocio.UpdateTarget(SinacorComboBox.SelectedValueProperty);
        }

        public void InformarNenhumaInconsistencia()
        {
            SinacorMessageBox.Show(FindResource("Bovespa.Ordens.Message.NenhumaInconsistencia").ToString(), SinacorMessageBoxButton.Ok, SinacorMessageBoxImage.Information);
        }

        public void SelecionarDataMovimento(Sinacor.UI.Bovespa.ManutencaoOrdens.Model.InformacoesMovimento model)
        {
            if (cmbErrosRecepcaoOfertaNegocio.Items.Count > 0 && model.DataMovimento > DateTime.MinValue)
            {
                cmbErrosRecepcaoOfertaNegocio.SelectedValue = model.DataMovimento.ToShortDateString();
            }
        }

        #endregion

        private void btnAcerto_OnBufferedClick(object sender, RoutedEventArgs e, int clickCount)
        {            
            this.verifica_acerto();
        }

        private void btnPesquisar_OnBufferedClick(object sender, RoutedEventArgs e, int clickCount)
        {
            base.ShowUserWaitingProcess();
            _presenter.ConsultarErros();            
            if (dtgOrdem.HasItems || dtgNegocio.HasItems)
            {
                this.btnAcerto.IsEnabled = true;
            }
            base.HideUserWaitingProcess();
        }

        private void Acerto_Click(object sender, RoutedEventArgs e)
        {         
            this.verifica_acerto();
        }

        private void Acerto_Loaded(object sender, RoutedEventArgs e)
        {

            SinacorContextMenuItem item = (SinacorContextMenuItem)sender;
            if (dtgNegocio.HasItems || dtgOrdem.HasItems)
            {
                item.IsEnabled = true;
            }
            else
            {
                item.IsEnabled = false;
            }
        }

        private void Pesquisar_Loaded(object sender, RoutedEventArgs e)
        {
            SinacorContextMenuItem item = (SinacorContextMenuItem)sender;
            if (cmbErrosRecepcaoOfertaNegocio.SelectedValue != null)
            {
                item.IsEnabled = true;
            }
            else
            {
                item.IsEnabled = false;
            }
        }

        private void Pesquisar_Click(object sender, RoutedEventArgs e)
        {
            base.ShowUserWaitingProcess();
            _presenter.ConsultarErros();
            base.HideUserWaitingProcess();
        }

        public void verifica_acerto()
        {            
            base.ShowUserWaitingProcess();
            _presenter.VerificarCorrecoesErros();
            base.HideUserWaitingProcess();
        } 
    }
}